import { Component, OnInit, Input } from '@angular/core';
import { Member, ClassMember } from '../Interfaces/Member';
import { ClassEntity } from '../Interfaces/ClassEntity';
import { MemberService } from '../../services/member.service';

@Component({
  selector: 'app-show-cell',
  templateUrl: './show-cell.component.html',
  styleUrls: ['./show-cell.component.css']
})
export class ShowCellComponent implements OnInit {


    memberList: ClassMember[];
    memberNameList: string[] = new Array();
    originMembers: ClassMember[];
    members: Member[];
    isOpen = false;

    constructor (
        private memberService: MemberService
    ) {}

    @Input('classEntity') classEntity: ClassEntity;
    ngOnInit() {
        console.log(this.classEntity);
        this.originMembers = JSON.parse(JSON.stringify(this.classEntity.members));
        this.initMember(this.classEntity.members, this.classEntity.member_limit );
    }

    initMember(members: ClassMember[], length: number) {
        this.memberList = members;
        this.memberList.map( res => {
            this.memberNameList.push( res.member_name) ;
        });
        let left_length = 0;
        if (members.length < length) {
            left_length = length - members.length;
        }

        for (let index = 0; index < left_length; index++) {
            this.memberList.push(new ClassMember());
        }
    }

    onShown() {
        console.log('show');
        const id_list = [];
        this.originMembers.map( res => {
            id_list.push(res.member_id);
        });

        this.memberService.getMembersByIds(id_list).subscribe(
            res => {
                console.log(res);
                if (!res.errors) {
                    this.members = res.data as Member[];
                }
        });

    }
}
